package com.example.blades.mynewdialer;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class NewDialerActivity extends AppCompatActivity {
    /**Called when the activity is first created.*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_dialer);

        Button dial=(Button) findViewById(R.id.dial);
        dial.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //Intent <Intent_name>=new Intent(<Action>,<Data>);
                Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel://114"));
                startActivity(intent);
            }
        });

        final EditText phoneNum=(EditText)findViewById(R.id.phoneNum);
        final Button button=(Button)findViewById(R.id.btn2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String call=phoneNum.getText().toString();
                if(PhoneNumberUtils.isGlobalPhoneNumber(call))
                {
                    if (ContextCompat.checkSelfPermission(NewDialerActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
                        return;
                    }
                    Intent i=new Intent(Intent.ACTION_CALL,Uri.parse("tel://"+call));
                    startActivity(i);
                }
                else
                {
                    Toast.makeText(NewDialerActivity.this,R.string.notify_incorrect_phonenum,Toast.LENGTH_SHORT).show();
                }
            }
        });

    }




}
